package snooker.scoreboard.button;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public abstract class AbstractThreeStateImage implements IButton {
	private ImageView imageView;
	
	private Bitmap resizedBitmap1;
	private Bitmap resizedBitmap2;
	private Bitmap resizedBitmap3;
	
	private boolean enabled = true;
	
	protected AbstractThreeStateImage(ImageView v, Resources r, int resource1, int resource2, int resource3) {
		this.imageView = v;
		resizedBitmap1 = BitmapFactory.decodeResource(r, resource1);
		resizedBitmap2 = BitmapFactory.decodeResource(r, resource2);
		resizedBitmap3 = BitmapFactory.decodeResource(r, resource3);
//		Bitmap originalBitmap1 = BitmapFactory.decodeResource(r, resource1);
//		Bitmap originalBitmap2 = BitmapFactory.decodeResource(r, resource2);
//		Bitmap originalBitmap3 = BitmapFactory.decodeResource(r, resource3);
//		int width = originalBitmap1.getWidth(); 
//		int height = originalBitmap1.getHeight(); 
//		int newWidth = 50;
//		int newHeight = 50; 
//		
//		// calculate the scale - in this case = 0.4f 
//		float scaleWidth = ((float) newWidth) / width; 
//		float scaleHeight = ((float) newHeight) / height; 
//		
//		// create a matrix for the manipulation 
//		Matrix matrix = new Matrix(); 
//		// resize the bit map 
//		matrix.postScale(scaleWidth, scaleHeight); 
//
//		// recreate the new Bitmap 
//		resizedBitmap1 = Bitmap.createBitmap(originalBitmap1, 0, 0, 
//							width, height, matrix, true);
//		resizedBitmap2 = Bitmap.createBitmap(originalBitmap2, 0, 0, 
//				  			width, height, matrix, true);
//		resizedBitmap3 = Bitmap.createBitmap(originalBitmap3, 0, 0, 
//	  			width, height, matrix, true);
		imageView.setImageBitmap(resizedBitmap1);
	}
	
	public void disable() {
		enabled = false;
		setState3();
	}
	
	public void enable() {
		enabled = true;
		setState1();
	}
	
	public boolean isEnabled() {
		return enabled;
	}
	
	protected void setState1() {
		imageView.setImageBitmap(resizedBitmap1);
	}
	
	protected void setState2() {
		imageView.setImageBitmap(resizedBitmap2);
	}

	protected void setState3() {
		imageView.setImageBitmap(resizedBitmap3);
	}
	
	protected void setOnTouchListener(OnTouchListener listener) {
		imageView.setOnTouchListener(listener);
	}
	
	protected void setOnClickListener(OnClickListener listener) {
		imageView.setOnClickListener(listener);
	}
}
